<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
    protected $guarded=[];
    public $timestamps=false;
    public static function getTree($pid,$level,$treeData=[]){
        GLOBAL $treeData;
        $rowData=Category::where('pid',$pid)->get();
        if($rowData->count()>0){
            foreach($rowData as $item){
                $item['level']=$level;
                $treeData[]=$item;
                if(Category::where('pid',$item->id)->first()){
                    Category::getTree($item->id,$level+1,$treeData);
                }
            }
        }else{
            $treeData=[];
        }
        return $treeData;
    }
}
